package com.atguigu.tingshu.common.delay;

import com.atguigu.tingshu.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.concurrent.TimeUnit;

/**
 * @author: atguigu
 * @create: 2024-04-24 10:45
 */
@Slf4j
@Component
public class DelayMessageService {


    @Autowired
    private RedissonClient redissonClient;


    /**
     * 发送延迟消息
     * @param queueName
     * @param data
     * @param ttl
     */
    public void sendDelayMsg(String queueName, String data, int ttl) {
        //1.基于Redisson客户端对象创建阻塞队列
        RBlockingQueue<String> blockingQueue = redissonClient.getBlockingQueue(queueName);
        //2.基于Redisson客户端对象将阻塞队列作为参数创建延迟队列
        RDelayedQueue<String> delayedQueue = redissonClient.getDelayedQueue(blockingQueue);
        //3.调用阻塞队列对象发送延迟消息
        delayedQueue.offer(data, ttl, TimeUnit.SECONDS);
        log.info("发送延迟消息到队列：{}成功：{}，延迟时间：{}s", queueName, data, ttl);
    }

}
